// 使用方法：
// 1.引入必要文件
// <link href="/assets/umpadmin/style.css" rel="stylesheet">
// 2.在html中添加：
// 2.1 启动读取： <a class="button js-load-items"><i class="icon-plus"></i>添加商品</a> 
// 2.2 保存已读取的ids：<input type="hidden" class="item-set-val" value="" />
// 2.3 显示已读取的商品数量：<div class="item-num" /></div>
// 2.4 显示已读取的商品列表 <ul class="clearfix js-added"></ul>
	Array.prototype.delRepeat = function () {
        this.sort();//排序
        var n = [this[0]];
        for (var i = 1; i < this.length; i++) {
            if (this[i] !== n[n.length - 1]) {
                n.push(this[i]);
            }
        }
        return n;
    }
	Array.prototype.remove = function(val) {  
		var index = this.indexOf(val);  
		if (index > -1) {  
			this.splice(index, 1);  
		}  
	};  
	BUI.use(['bui/calendar','bui/overlay','bui/form'],function(Calendar,Overlay,Form){
	var loading="<div class=\"loading\" style=\"width:34px;height:34px;margin:100px auto;background:url()\"></div>";
	var itemdialog = new Overlay.Dialog({
		title:'选择商品',
		width:990,
		height:600,
		mask:false,
		content:"<div class=\"load\" ></div>"
	});
	var q="";
	var item_iids=new Array();
	$(".js-load-items").on("click",function(){
		itemdialog.show();
		itemdialog.set("y",$(document).scrollTop());
		itemdialog.set("zIndex",10000);
		$(loading).appendTo(".load");
		var item_value = $(".item-set-val").val();
		if(item_value){
			item_iids = item_value.split(',');
		}else{
			item_iids=new Array();
		}
		$.ajax(
		{"url":"/?m=umpadmin&a=load_items",	
			"data":{"q":q,"pg":$(".bui-pb-page").val()},
			"dataType": "json",
			"success":function(data,status){
				var isfirst=(data.pg==1?true:false);
				var islast =(data.pg==data.sumpages?true:false);
				var items = "";
				    items +="<div class=\"bui-grid \" style=\"margin:10px 50px;clear:both\" >";
					items +='<ul class="bui-pagingbar bui-bar pull-left" role="toolbar" id="bar3" aria-disabled="false" aria-pressed="false"><li class="bui-bar-item-button bui-bar-item bui-pb-first bui-inline-block '+(isfirst?'bui-bar-item-button-disabled bui-bar-item-disabled':'')+'" aria-disabled="true" id="first"><button type="button" '+(isfirst?'class="bui-button-disabled" disabled="disabled"':'')+'>首 页</button></li><li class="bui-bar-item-button bui-bar-item bui-pb-prev bui-inline-block '+(isfirst?'bui-bar-item-button-disabled bui-bar-item-disabled':'')+'" aria-disabled="true" id="prev"><button type="button" '+(isfirst?'class="bui-button-disabled" disabled="disabled"':"")+'>上一页</button></li><li class="bui-bar-item-separator bui-bar-item bui-inline-block" aria-disabled="false" id="bar-item-separator1" role="separator"></li><li class="bui-bar-item-text bui-bar-item bui-inline-block" aria-disabled="false" id="totalPage" aria-pressed="false">共 '+data.sumpages+' 页</li><li class="bui-bar-item-text bui-bar-item bui-inline-block" aria-disabled="false" id="curPage">第 <input type="text" autocomplete="off" class="bui-pb-page" size="20" value="'+data.pg+'" name="inputItem"> 页</li><li class="bui-bar-item-button bui-bar-item bui-pb-skip bui-inline-block" aria-disabled="false" id="skip"><button type="button" class="">确定</button></li><li class="bui-bar-item-separator bui-bar-item bui-inline-block '+(isfirst?'bui-bar-item-button-disabled bui-bar-item-disabled':'')+'" aria-disabled="false" id="bar-item-separator2" role="separator"></li><li class="bui-bar-item-button bui-bar-item bui-pb-next bui-inline-block '+(isfirst?'bui-bar-item-button-disabled bui-bar-item-disabled':'')+'" aria-disabled="false" id="next"><button type="button" '+(islast?'class="bui-button-disabled" disabled="disabled"':'')+'>下一页</button></li><li class="bui-bar-item-button bui-bar-item bui-pb-last bui-inline-block '+(islast?'bui-bar-item-button-disabled bui-bar-item-disabled':'')+'" aria-disabled="false" id="last"><button type="button" '+(islast?'class="bui-button-disabled" disabled="disabled"':'')+'>末 页</button></li><li class="bui-bar-item-separator bui-bar-item bui-inline-block" aria-disabled="false" id="bar-item-separator3" role="separator"></li><li class="bui-bar-item-text bui-bar-item bui-inline-block" aria-disabled="false" id="totalCount">共'+data.total_results+'条记录</li></ul>';
					items +="<i><input type=\"text\" class=\"input-small q \" value=\""+q+"\" /><button class=\"button button-mini js-search\">搜索</button></i></div>";
					items +="<ul class=\"clearfix \" style=\"height:600px;padding:0 0 10px 40px;overflow-y:scroll;\">";
				for(var i=0;i<data.items.item.length;i++){
					var btncls = item_iids.indexOf(data.items.item[i].num_iid.toString())>=0?"button-danger":"button-primary";
					var btntxt = item_iids.indexOf(data.items.item[i].num_iid.toString())>=0?"已选择":"选择";
					items+='<li class="item item'+data.items.item[i].num_iid+'">';
					items+='<input type="hidden" name="price[]" value="'+data.items.item[i].price+'" />';
					items+='<input type="hidden" name="item_iid[]" value="'+data.items.item[i].num_iid+'" />';
					items+='<input type="hidden" name="item_pic_url[]" value="'+data.items.item[i].pic_url+'" />';
					items+='<input type="hidden" name="item_title[]" value="'+data.items.item[i].title+'" />';
					items+='<dl><img src="'+data.items.item[i].pic_url+'_120x120"  /></dl>';
					items+='<ol>'+data.items.item[i].title+'</ol>';
					items+='<ol><button class="button buttom-mini '+btncls+' js-select" data-iid="'+data.items.item[i].num_iid+'" type=\"button\">'+btntxt+'</button></ol>';
					items+='</li>';
				}
				items+="</ul>";
				items+='<div class="centered" style="margin-bottom:10px"><button class=\"button button-danger js-close \">确定</button></div>';
				$(".load").empty();
				$(".loading").remove();
				$(items).appendTo(".load");
				$(".js-select").on("click",function(){
					var curiid = $(this).attr("data-iid");
					if(item_iids.length>50){
						alert("每个活动最多选择50个商品！");
						return;
					}
					if(item_iids.indexOf(curiid)>=0){						
						$(this).removeClass("button-danger");
						$(this).addClass("button-primary");
						$(this).text("选择");
						item_iids.remove(curiid);
						selected_items(curiid,"remove");
					}else{
						$(this).removeClass("button-primary");
						$(this).addClass("button-danger");
						$(this).text("已选择");
						item_iids.push(curiid);
						item_iids = item_iids.delRepeat();
						selected_items(curiid,"add");
					}
					$(".item-set-val").val(item_iids.join(","));
					$(".item-num").text("已选择"+item_iids.length+"个商品");
				});
				$(".js-search").on("click",function(){
					q = $(".q").val();
					$(".js-load-items").click();
				});
				$(".bui-pb-next").on("click",function(){
					var curpg = $(".bui-pb-page").val();
					var pg = curpg<data.sumpages?curpg++:data.sumpages;
					$(".bui-pb-page").val(curpg<data.sumpages?curpg++:data.sumpages);
					$(".js-load-items").click();
				});
				$(".bui-pb-last").on("click",function(){
					$(".bui-pb-page").val(data.sumpages);
					$(".js-load-items").click();
				});
				$(".bui-pb-prev").on("click",function(){
					var curpg = $(".bui-pb-page").val();
					$(".bui-pb-page").val(curpg>1?curpg-1:1);
					$(".js-load-items").click();
				});
				$(".bui-pb-first").on("click",function(){
					$(".bui-pb-page").val(1);
					$(".js-load-items").click();
				});
				$(".bui-pb-skip").on("click",function(){
					$(".js-load-items").click();
				});
				$(".js-close").on("click",function(){
					itemdialog.close();
				});
			}
		});
	});
	function selected_items(item_iid,ac){
		if($(".js-added")){
			if(ac=="add"){
				var item = $(".item"+item_iid).clone(true);
				item.removeClass("item"+item_iid);
				item.addClass("add_item_"+item_iid);
				item.children("ol").children(".js-select").text("删除");
				$(".js-added").append(item);
			}
			if(ac=="remove"){
				$(".add_item_"+item_iid).remove();
			}
		}
	}
});